var ReviveDlg = (function(){
    function ReviveDlg(_confirmHandler,_cancleHandler) {
        ReviveDlg.super(this);
        this.confirmHandler = _confirmHandler;
        this.cancleHandler = _cancleHandler;
        this.addBlackLayer();
        Laya.timer.frameOnce(30,this,function () {
            this.btnRevive.on(Laya.Event.MOUSE_DOWN, this, this.onRelive);
            this.btnCancle.on(Laya.Event.MOUSE_DOWN, this, this.onCancel);
        });
        
       //分数
       this.score.text = Config.gameScore;
       var hisScore = BaseData.getGameScore(Config.gameScoreKey);
       this.his_score.text = "历史最高分:" + hisScore;
       if(Config.gameScore > hisScore){
           BaseData.saveGameScore(Config.gameScore);
       }

       this.showAd = false;
       this.isUpdate = true;
       this.timeCount = 10;
       this.timeStep = 0;
       this.isUp = true;
       Laya.timer.frameLoop(1,this,this.timeLoop);
       BaseData.initSceneGameData(this)

        //关闭音乐
        Audio.pauseAll();
    }

    Laya.class(ReviveDlg, 'ui.reviveDlg', ReviveUI);
    
    ReviveDlg.prototype.timeLoop = function(){
        if(!this.isUpdate)return;
        this.timeStep += 1;
        this.move.rotation += 6;
        if(this.isUp && this.hurt.scaleX < 1){
            this.hurt.scaleX += 0.01;
            this.hurt.scaleY += 0.01;
        }else{
            this.isUp = false;
            this.hurt.scaleX -= 0.01;
            this.hurt.scaleY -= 0.01;
            if(this.hurt.scaleX < 0.8){
                this.isUp = true;
            }
        }

        if(this.timeCount > 0){
           if(this.timeStep % 60 == 0){
               this.timeCount -= 1;
               this.hurt.skin = "ads/n0"+this.timeCount+".png"
           }else{
               
           }
        }else{
           this.timeCount = 0;
           this.hurt.skin = "ads/n0"+this.timeCount+".png"
           Laya.timer.clear(this,this.timeLoop);
           this.onCancel();
        }
    };

    ReviveDlg.prototype.onRelive = function(){
        //弹出广告
        var that = this;

        that.isUpdate = false;
        Laya.timer.clear(that,that.timeLoop);
        if(Config.isAd){
            if(Config.isLoadSuccess){
                AdUtil.addVideoListener(function(){
                    that.close();
                    that.confirmHandler();
                },function(){
                    that.onCancel();
                });
                AdUtil.showVideo();
            }else{
                if(Config.channelID == 1 && wx.showToast){
                    var errTips = new ErrTips("暂无视频可看哦...",2000)
                }
                //
                that.onCancel();
            }
        }else{
            that.confirmHandler();
        }
    };

    ReviveDlg.prototype.onCancel = function(){
        this.isUpdate = false;
        Laya.timer.clear(this,this.timeLoop);
        Audio.resumeAll();
        this.close();
        this.cancleHandler();
        
        Config.bannerAd.hide();
    };

    ReviveDlg.prototype.showDlgAndMask = function(){
        this.popup(true);
    };

    ReviveDlg.prototype.addBlackLayer = function(){
        this.bg.height += offH;
        this.bg.y -= offH / 2;
        var blackMat =
        [
            0, 0, 0, 0, 0, //R
            0, 0, 0, 0, 0, //G
            0, 0, 0, 0, 0, //B
            0, 0, 0, 0.6, 0, //A
        ];
        //创建一个颜色滤镜对象,黑色
        var blackFilter = new Laya.ColorFilter(blackMat);
        this.bg.filters = [blackFilter];
    }
    return ReviveDlg;
})();